package com.aisile.sellergoods.service.impl;

import com.aisile.entity.PageResult;
import com.aisile.mapper.TbBrandMapper;
import com.aisile.pojo.TbBrand;
import com.aisile.pojo.TbBrandExample;
import com.aisile.sellergoods.service.BrandService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    TbBrandMapper tbBrandMapper;

    @Override
    public List<TbBrand> findAll() {

        return tbBrandMapper.selectByExample(null);
    }

    @Override
    public PageResult findPage(int page, int rows,TbBrand brand) {
        PageHelper.startPage( page, rows);
        String name=brand.getName();
        String firstChar=brand.getFirstChar();


        TbBrandExample tbBrandExample=new TbBrandExample();
        if(name!=null && !"".equals(name)){
            tbBrandExample.createCriteria().andNameLike("%"+brand.getName()+"%");

        }
        if(firstChar!=null && !"".equals(firstChar)){

            tbBrandExample.createCriteria().andNameLike("%"+brand.getFirstChar()+"%");
        }


        Page<TbBrand> pageResult=(Page<TbBrand>) tbBrandMapper.selectByExample(tbBrandExample);
        return new PageResult(pageResult.getTotal(),pageResult.getResult());
    }

    @Override
    public void save(TbBrand tbBrand) {

        tbBrandMapper.insert(tbBrand);

    }

    @Override
    public TbBrand findOne(long id) {
        return tbBrandMapper.selectByPrimaryKey(id);
    }

    @Override
    public void update(TbBrand tbBrand) {

        tbBrandMapper.updateByPrimaryKey(tbBrand);
    }

    @Override
    public void dele(Long[] ids) {

        if(ids!=null){

            for(Long id:ids){
                tbBrandMapper.deleteByPrimaryKey(id);

            }
        }

    }

    @Override
    public List<Map> findBrandList() {

        return tbBrandMapper.findBrandList();


    }


}
